import type { TreeNode } from '@vben/types';
import { requestClient } from '#/api/request';

export namespace EquityDicApi {
  export interface PageFetchParams {
    [key: string]: any;
    page: number;
    pageSize: number;
  }

  export interface SaveParams {
    id?: number;
    pid: number;
    name: string;
    ratio: number;
    status: string;
    sort: number;
    description: string;
  }

  export interface DragDropParams {
    dragId: number;
    dropId: number;
    dropPosition: number;
    dropType: 'same' | 'sub';
  }
}

export interface EquityDicData {
  id?: number;
  pid: number;
  name: string;
  ratio: number;
  status: string;
  sort: number;
  description: string;
}

/**
 * 获取权益字典列表
 */
export function getEquityDicListApi(params: EquityDicApi.PageFetchParams) {
  return requestClient.get('/partner/equity-dic/list', { params });
}

/**
 * 获取权益字典树
 */
export async function getEquityDicTreeApi() {
  return requestClient.get<TreeNode[]>('/partner/equity-dic/tree');
}

/**
 * 保存权益字典
 */
export function saveEquityDicApi(data: EquityDicApi.SaveParams) {
  return requestClient.post('/partner/equity-dic/save', data);
}

/**
 * 删除权益字典
 */
export function deleteEquityDicApi(id: number | string) {
  return requestClient.delete(`/partner/equity-dic/delete?id=${id}`);
}

/**
 * 获取权益字典详情
 */
export async function getEquityDicDetailApi(id: number | string) {
  return requestClient.get<EquityDicData>(`/partner/equity-dic/${id}`);
}

/**
 * 搜索权益字典
 */
export async function searchEquityDicApi(name: string) {
  return requestClient.get<TreeNode[]>(`/partner/equity-dic/search?name=${name}`);
}

/**
 * 更新权益字典节点位置
 */
export function updateEquityDicPositionApi(params: EquityDicApi.DragDropParams) {
  return requestClient.post('/partner/equity-dic/move', params);
}

/**
 * 批量更新权益字典排序
 */
export function updateEquityDicOrderApi(data: { id: number; order: number }[]) {
  return requestClient.post('/partner/equity-dic/updateOrder', data);
}